import re
import time
from time import sleep

from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.common import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from base.base_page import BasePage
from utils.log import logger



class PhotoAlbumPage(BasePage):

    def open_file_btn(self):
        return self.by_uiautomator('new UiSelector().text("文档导入")').click()

    # 拍摄页面相册导入按钮
    def photo_album_import_btn(self):
        return self.by_id(self.HW_ENV + 'image_open_gallery1').click()

    # 相册页面导入按钮
    def commit_btn(self):
        return self.by_id(self.HW_ENV + 'commit').click()

    # 相册选择按钮
    def album_select_btn(self):
        return self.by_id(self.HW_ENV + 'selected_album').click()

    # 自动裁剪按钮
    def auto_cut_btn(self):
        return self.by_uiautomator('new UiSelector().text("自动裁剪")').click()

    # 旋转按钮
    def rotate_btn(self):
        return self.by_accessibility_id('旋转').click()

<<<<<<< HEAD
    def btn_2_complete(self):
        return self.by_id(self.HW_ENV + 'btn_2_complete').click()
=======
    # 预览按钮
    def preview_btn(self):
        return self.by_uiautomator('new UiSelector().text("预览")').click()
>>>>>>> 41acbbe (commit)

    # 下一步按钮
    def next_btn(self):
        return self.by_id(self.HW_ENV + 'btn_complete').click()

    def album_selected(self, album_name):
        """
        根据相册名选择相册
        :param album_name:相册名
        :return:点击相册事件
        """
        sleep(1)
        if self.is_element(album_name):
            return self.by_uiautomator('new UiSelector().textContains("' + album_name + '")').click()
        else:
            while 1:
                if self.is_element(album_name):
                    return self.by_uiautomator('new UiSelector().textContains("' + album_name + '")').click()
                    break
                else:
                    self.drag(0.5, 0.5, 0.4, 0.1, 800)

    def choose(self, num):
        return self.by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout[' + str(num) + ']/android.widget.ImageView')

    def pictures_choose(self, album_name, nums):
        """
        根据相册名与图片数量选择多张图片

        :param album_name: 相册名
        :param nums:图片数量
        :return:none
        """
        self.driver.implicitly_wait(5)
        self.album_select_btn()
        self.driver.implicitly_wait(5)
        self.album_selected(album_name)  # 选择相册
        self.driver.implicitly_wait(10)
        image_elements = self.by_uiautomators('new UiSelector().resourceId("' + self.HW_ENV + 'media_thumbnail")')
        if nums < 15:
            for i in range(0, nums):
<<<<<<< HEAD
                try:
                    image_elements[i].click()
                except NoSuchElementException:
                    self.take_screenshot("相册选择图片异常")
=======
                image_elements[i].click()
>>>>>>> 41acbbe (commit)
        else:
            visible_bounds = image_elements[1].get_attribute('bounds')
            visible_bounds_list = re.findall(r'\d+', visible_bounds)
            visible_height = int(visible_bounds_list[3]) - int(visible_bounds_list[1])

            not_visible_bounds = image_elements[-1].get_attribute('bounds')
            not_visible_bounds_list = re.findall(r'\d+', not_visible_bounds)
            not_visible_height = int(not_visible_bounds_list[3]) - int(not_visible_bounds_list[1])
            if not_visible_height < visible_height:
                visible_image_elements = image_elements[:len(image_elements) - 3]
            num_of_page = len(visible_image_elements)
            page_num = int(nums / num_of_page)
            remainder = nums % num_of_page

            for i in range(0, page_num):
                for j in range(0, num_of_page):
                    image_elements[j].click()
                start_x = image_elements[-2].location['x']
                start_y = image_elements[-2].location['y']
                end_x = image_elements[1].location['x']
                end_y = image_elements[1].location['y']
                action = TouchAction(self.driver)
                action.long_press(x=start_x, y=start_y).move_to(x=end_x, y=end_y).release().perform()
            if remainder:
                image_count = len(image_elements)
                for i in range(image_count - remainder, image_count):
                    image_element = image_elements[i]
                    image_element.click()

        self.commit_btn()
        self.driver.implicitly_wait(5000)

    def picture_choose(self, album_name, index):
        """
        根据相册名与索引选择单张图片
        :param album_name: 相册名
        :param index: 图片索引
        :return: none
        """
        self.album_select_btn()
        self.driver.implicitly_wait(10)
        self.album_selected(album_name)
        image_elements = self.by_uiautomators('new UiSelector().resourceId("' + self.HW_ENV + 'media_thumbnail")')
        image_elements[index - 1].click()
        sleep(1)
        self.commit_btn()
        self.driver.implicitly_wait(5)
